package leetcode.editor.cn.q1_300.q150;

import java.util.Arrays;

/**
 * 怎么说呢。。。这是个数学题啊。。。完全没看懂答案
 *
 * cv了
 *
 * @author qidi
 * @date 2022-02-18 15:30:04
 */
class Q0179_LargestNumber {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String largestNumber(int[] nums) {
            int n = nums.length;
            // 转换成包装类型，以便传入 Comparator 对象（此处为 lambda 表达式）
            Integer[] numsArr = new Integer[n];
            for (int i = 0; i < n; i++) {
                numsArr[i] = nums[i];
            }

            Arrays.sort(numsArr, (x, y) -> {
                long sx = 10, sy = 10;
                while (sx <= x) {
                    sx *= 10;
                }
                while (sy <= y) {
                    sy *= 10;
                }
                return (int) (-sy * x - y + sx * y + x);
            });

            if (numsArr[0] == 0) {
                return "0";
            }
            StringBuilder ret = new StringBuilder();
            for (int num : numsArr) {
                ret.append(num);
            }
            return ret.toString();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0179_LargestNumber().new Solution();
        System.out.println(solution.largestNumber(new int[]{34323, 3432}));
        System.out.println(solution.largestNumber(new int[]{10, 2}));
        System.out.println(solution.largestNumber(new int[]{20, 2, 21}));
        System.out.println(solution.largestNumber(new int[]{3, 30, 34, 5, 9}));
    }
}